package pwd.allen.androiddemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import pwd.allen.androiddemo.R;

import java.util.Date;

public class ReturnActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_param;

    /**
     * 输入框
     * 隐藏输入法软键盘的方法：
     * ViewUtil.hideOneInputMethod(this, et_param);
     */
    private EditText et_param;

    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return);
        tv_param = findViewById(R.id.tv_param);
        et_param = findViewById(R.id.et_param);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            tv_param.setText("上一页传的param属性：" + bundle.getString("param"));
        }
        et_param.setText("返回结果：" + new Date().toString());

        findViewById(R.id.btn_rel).setOnClickListener(this);
        findViewById(R.id.btn_back).setOnClickListener(this);
        findViewById(R.id.btn_send).setOnClickListener(this);

        // 注册返回到这个页面时回调的方法，需要在onStart之前注册完成； androidx 1.3.0之后版本支持
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            //回调函数
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null) {
                    Intent intent = result.getData();
                    if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
                        //获取到返回的数据
                        Bundle bundle = intent.getExtras();
                        String val = bundle.getString("param");
                        Toast.makeText(ReturnActivity.this, String.format("获取页面返回结果: %s", val), Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_rel:
                returnParam();
                break;
            case R.id.btn_send:
                //<editor-fold desc="跳转到myActivity，并在返回时处理其返回结果">
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("param", et_param.getText().toString());
                intent.putExtras(bundle);
                intent.setClass(this, ReturnActivity.class);
                register.launch(intent);
                // 已废弃
//                startActivityForResult(intent, Activity.RESULT_OK);
                //</editor-fold>
                break;
        }
    }

    /**
     * 给上个页面返回结果
     */
    public void returnParam() {
        Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putString("param", et_param.getText().toString());
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK, intent);
    }
}
